<template>
  <business-layout title='操作票模板'>
    <!--        <div class='table-operator' slot='query'>-->
    <!--          <a-form :form='form1' layout='inline' @submit='handleQueryData'>-->
    <!--            <a-form-item label='模板类型'>-->
    <!--              <a-select style='width: 140px;' placeholder='请选择模板类型' v-decorator="['site']">-->
    <!--                <a-select-option value='0'>类型1</a-select-option>-->
    <!--                <a-select-option value='1'>类型2</a-select-option>-->
    <!--                <a-select-option value='2'>类型3</a-select-option>-->
    <!--              </a-select>-->
    <!--            </a-form-item>-->
    <!--            <a-form-item label='状态'>-->
    <!--              <a-select style='width: 140px;' placeholder='请选择状态' v-decorator="['source']">-->
    <!--                <a-select-option value='0'>状态1</a-select-option>-->
    <!--                <a-select-option value='1'>状态2</a-select-option>-->
    <!--                <a-select-option value='2'>状态3</a-select-option>-->
    <!--              </a-select>-->
    <!--            </a-form-item>-->
    <!--            <a-form-item label='编号或者名称'>-->
    <!--              <a-input placeholder='请输入编号或者名称' v-decorator='["keywords"]' />-->
    <!--            </a-form-item>-->
    <!--            <a-form-item>-->
    <!--              <a-button type='primary' html-type='submit'>-->
    <!--                <a-icon type='search' />-->
    <!--                查询-->
    <!--              </a-button>-->

    <!--              <a-button>-->
    <!--                重置-->
    <!--              </a-button>-->
    <!--            </a-form-item>-->
    <!--          </a-form>-->
    <!--        </div>-->

    <div slot='btns'>
      <a-button type='primary' @click='handleAdd'>新增操作票</a-button>
    </div>

    <s-table
      ref='table'
      size='default'
      rowKey='key'
      :columns='columns'
      :data='loadData'
      :alert='false'
      :showPagination='false'
    >
      <span slot='serial' slot-scope='text, record, index'>
        {{ index + 1 }}
      </span>
      <span slot='description' slot-scope='text'>
        <ellipsis :length='4' tooltip>{{ text }}</ellipsis>
      </span>

      <span slot='action' slot-scope='text, record'>
        <template>
          <a @click='handleSub(record)'>预览</a>
        </template>
      </span>
    </s-table>

    <create-form
      ref='createModal'
      :visible='visible'
      :loading='confirmLoading'
      :model='mdl'
      @cancel='handleCancel'
      @ok='handleOk'
    />

    <previwe-file
      ref='previewfile'
      :previewVisible='previewVisible'
      :previewLoading='previewLoading'
      @cancel='handlepreviewCancel'
      @ok='handlepreviewOk'
    />
  </business-layout>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'

import CreateForm from '../modules/CreateWorkOrderForm.vue'
import previweFile from '@/views/maintenance/modules/PreviewFile.vue'

const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '名称',
    dataIndex: 'name'
  },
  {
    title: '编码',
    dataIndex: 'code'
  },
  {
    title: '描述',
    dataIndex: 'description'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    previweFile
  },
  data () {
    this.columns = columns
    return {
      activeKey: '1',
      // create model
      visible: false,
      previewVisible: false,
      previewLoading: false,
      confirmLoading: false,
      form1: this.$form.createForm(this),
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return new Promise((resolve, reject) => {
          resolve({
            list: [{
              name: '换氢车操作票',
              description: '换氢车操作票',
              code: 'hydrogen'
            }],
            pageNo: 1,
            total: 1
          })
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {},
  created () {
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    // 预览操作票
    handlepreviewCancel () {
      this.previewVisible = false
    },
    handlepreviewOk () {
      this.previewVisible = false
    },
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('更新成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub (record) {
      this.previewVisible = true
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleQueryData () {

    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
<style lang='less' scoped>
.table-tab {

}
</style>
